package com.example.restaurantclient.Activity.Shop;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.restaurantclient.Adapter.Shopadapter;
import com.example.restaurantclient.ConfigUtil.ConfigUtil;
import com.example.restaurantclient.Entities.Shop;
import com.example.restaurantclient.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class ShopActivity extends AppCompatActivity {
    private List<Shop> shops = new ArrayList<>();
    private Shopadapter shopadapter;
    private TextView tvInte;
    private LinearLayout llRecord;
    private ImageView back;
//    private SmartRefreshLayout refreshLayout;
    private final int REFRESH = 100;
    private GridView stuListView ;
    private Handler mainHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
//                case REFRESH:
//                    shops.clear();
//                    initData();
//                    initView();
//                    //结束刷新动画
//                    refreshLayout.finishRefresh();
//                    break;
                case 200:
//                    shopadapter = new Shopadapter(ShopActivity.this,shops,R.layout.shopitem);
//                    stuListView.setAdapter(shopadapter);
////                    initView();
//                    Log.e("123456776543","23456765");
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        tvInte = findViewById(R.id.tv_integral);
        llRecord = findViewById(R.id.ll_record);
        back = findViewById(R.id.mine_back);
//        refreshLayout=findViewById(R.id.sr2);
        stuListView = findViewById(R.id.gv_list);
//        if (Variable.loginIdentity == 1){
//            tvInte.setText(Variable.teacher.getPoints());
//        }else {
            tvInte.setText(100000+"");
//        }
//        setListener();
        initData();
        shopadapter = new Shopadapter(ShopActivity.this,shops,R.layout.shopitem);
        stuListView.setAdapter(shopadapter);
//        initData();
        initView();


        llRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("商城页面的所有商品信息",shops.toString());
                Intent intent = new Intent();
                intent.setClass(ShopActivity.this,ShopRecordActivity.class);
                startActivity(intent);

            }
        });
    }
//
//
    public void initView(){

        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(ShopActivity.this, ShopShop.class);
                intent.putExtra("id",shops.get(position).getGoods_id());
                intent.putExtra("name",shops.get(position).getGoods_name());
                intent.putExtra("price",shops.get(position).getGoods_price());
                intent.putExtra("picture",shops.get(position).getDynamicImgs());
                intent.putExtra("amount",shops.get(position).getGoods_amount());
                intent.putExtra("integral",shops.get(position).getGoods_integral());
                intent.putExtra("no",shops.get(position).getGoods_no());
                intent.putExtra("size",shops.get(position).getGoods_size());
                intent.putExtra("material",shops.get(position).getGoods_material());
                intent.putExtra("explain",shops.get(position).getGoods_size());
                intent.putExtra("delivery",shops.get(position).getGoods_delivery());
                startActivity(intent);
            }
        });
        shopadapter.notifyDataSetChanged();
    }
//
    public void initData(){

        Shop shop=new Shop();
        shop.setGoods_id(1);
        shop.setGoods_name("iphone12");
        shop.setGoods_price(7599);
        shop.setGoods_amount(70);
        shop.setGoods_integral(75990);
        shop.setGoods_no("001");
        shop.setGoods_size("6.1英寸");
        shop.setGoods_material("苹果手机");
        shop.setGoods_explain("超级视网膜XDR显示屏，分辨率为2532x1170，对比度为200万：1，亮度高达1200尼特，支持杜比视界和HDR10。采用六核A14 Bionic芯片。");
        shop.setGoods_delivery("全国（可配送至全国100多个城市）");
        shop.setDynamicImgs("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2FQN3gupeQ3yLHDglR10k2EA.jpg_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648978561&t=4fe4206af44658af2fbfd03c8c96954a");
        shops.add(shop);

        Shop shop1=new Shop();
        shop1.setGoods_id(2);
        shop1.setGoods_name("猕猴桃");
        shop1.setGoods_price(80);
        shop1.setGoods_amount(16);
        shop1.setGoods_integral(80);
        shop1.setGoods_no("002");
        shop1.setGoods_size("500g");
        shop1.setGoods_material("水果");
        shop1.setGoods_explain("北纬35度是猕猴桃离太阳最近的一端");
        shop1.setGoods_delivery("全国（可配送至全国100多个城市）");
        shop1.setDynamicImgs("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnyncj.qz.gov.cn%2Fpicture%2F-1%2F210823161448921797.png&refer=http%3A%2F%2Fnyncj.qz.gov.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648978642&t=6f11d63c860b7f6721efacd54d1e6140");
        shops.add(shop1);

        Shop shop2=new Shop();
        shop2.setGoods_id(3);
        shop2.setGoods_name("Lenovo拯救者");
        shop2.setGoods_price(8999);
        shop2.setGoods_amount(67);
        shop2.setGoods_integral(89990);
        shop2.setGoods_no(003+"");
        shop2.setGoods_size("15.6英寸");
        shop2.setGoods_material("联想电脑");
        shop2.setGoods_explain("RTX光追显卡+16G超大存储+100%sRGB色域屏！");
        shop2.setGoods_delivery("全国（可配送至全国100多个城市）");
        shop2.setDynamicImgs("https://img0.baidu.com/it/u=2877178198,2589806287&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500");
        shops.add(shop2);

        Shop shop3=new Shop();
        shop3.setGoods_id(4);
        shop3.setGoods_name("黑龙江大米");
        shop3.setGoods_price(108);
        shop3.setGoods_amount(39);
        shop3.setGoods_integral(1080);
        shop3.setGoods_no(004+"");
        shop3.setGoods_size("2.5kg");
        shop3.setGoods_material("黑龙江稻香米");
        shop3.setGoods_explain("黑龙江稻花香大米  特农有机米，全程有机种植，无农药，无添加剂");
        shop3.setGoods_delivery("全国（可配送至全国100多个城市）");
        shop3.setDynamicImgs("http://t15.baidu.com/it/u=1957880286,2377540647&fm=224&app=112&f=JPEG?w=500&h=500");
        shops.add(shop3);

    }
}
